package com.treasure.service.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * @Description: 
 * @version 1.0 
 * @author panxinwen 2018/11/2 16:38
 * @Copyright © 2015 | 武汉京航科技有限公司 Email:19919866605@163.com
 * */

public class DateUtil {
  /**定义常量**/
  public static final String DATE_YYYY="yyyy";
  public static final String DATE_JFP_STR="yyyyMM";
  public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
  public static final String DATE_SMALL_STR = "yyyy-MM-dd";
  public static final String DATE_SMALL_DD = "yyyyMMdd";
  public static final String DATE_KEY_STR = "yyyyMMddHHmmss";

  /**
   * 使用预设格式提取字符串日期
   * @param strDate 日期字符串
   * @return
   */
  public static Date parse(String strDate) {
    return parse(strDate,DATE_FULL_STR);
  }

  /**
   * 使用用户格式提取字符串日期
   * @param strDate 日期字符串
   * @param pattern 日期格式
   * @return
   */
  public static Date parse(String strDate, String pattern) {
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    try {
      return df.parse(strDate);
    } catch (ParseException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 两个时间比较
   * @param date1
   * @return
   */
  public static int compareDateWithNow(Date date1){
    Date date2 = new Date();
    int rnum =date1.compareTo(date2);
    return rnum;
  }

  /**
   * 两个时间比较(时间戳比较)
   * @param date1
   * @return
   */
  public static int compareDateWithNow(long date1){
    long date2 = dateToUnixTimestamp();
    if(date1>date2){
      return 1;
    }else if(date1<date2){
      return -1;
    }else{
      return 0;
    }
  }


  /**
   * 获取系统当前时间
   * @return
   */
  public static String getNowTime() {
    SimpleDateFormat df = new SimpleDateFormat(DATE_FULL_STR);
    return df.format(new Date());
  }
  
	/**
	 * @Title :getCurrWeekStartDate
	 * @param :@return
	 * @return :String
	 * @Description: 获取本周开始时间
	 */
	public static String getCurrWeekStartDate(String type) {
		Date date = new Date();
		if (date == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayofweek == 1) {
			dayofweek += 7;
		}
		cal.add(Calendar.DATE, 2 - dayofweek);
		return getDayStartTime(cal.getTime(),type);
	}

	// 获取某个日期的开始时间
	public static String getDayStartTime(Date d,String type) {
		Calendar calendar = Calendar.getInstance();
		if (null != d)
			calendar.setTime(d);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
				0, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return formateDate(calendar.getTime(), type);
	}
  /**
   * 获取系统当前时间
   * @return
   */
  public static String getNowTime(String type) {
    SimpleDateFormat df = new SimpleDateFormat(type);
    return df.format(new Date());
  }

  public static String formateDate(Date date,String type){
    SimpleDateFormat df = new SimpleDateFormat(type);
    return df.format(date);
  }

  /**
   * 获取系统当前计费期
   * @return
   */
  public static String getJFPTime() {
    SimpleDateFormat df = new SimpleDateFormat(DATE_JFP_STR);
    return df.format(new Date());
  }

  /**
   * 将指定的日期转换成Unix时间戳
   * @param  date 需要转换的日期 yyyy-MM-dd HH:mm:ss
   * @return long 时间戳
   */
  public static long dateToUnixTimestamp(String date) {
    long timestamp = 0;
    try {
      timestamp = new SimpleDateFormat(DATE_FULL_STR).parse(date).getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return timestamp;
  }

  /**
   * 将指定的日期转换成Unix时间戳
   * @param  date 需要转换的日期 yyyy-MM-dd
   * @return long 时间戳
   */
  public static long dateToUnixTimestamp(String date, String dateFormat) {
    long timestamp = 0;
    try {
      timestamp = new SimpleDateFormat(dateFormat).parse(date).getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return timestamp;
  }

  /**
   * 将当前日期转换成Unix时间戳
   * @return long 时间戳
   */
  public static long dateToUnixTimestamp() {
    long timestamp = new Date().getTime();
    return timestamp;
  }


  /**
   * 将Unix时间戳转换成日期
   * @return String 日期字符串
   */
  public static String unixTimestampToDate(long timestamp) {
    SimpleDateFormat sd = new SimpleDateFormat(DATE_FULL_STR);
    sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    return sd.format(new Date(timestamp));
  }

  public static String getNowTimeStamp() {
    long time = System.currentTimeMillis();
    String nowTimeStamp = String.valueOf(time / 1000);
    return nowTimeStamp;
  }

  public static Long getTimestampType(Integer type){

    Long timeStamp= Long.parseLong(getNowTime(DATE_SMALL_DD));

    switch (type){
      case 0:
        timeStamp=20181215l;
        break;
      case 2:
        timeStamp=Long.parseLong(getWeekMonday());
        break;
      case 3:
        timeStamp=Long.parseLong(getMonthFirst());
        break;
      case 4:
        timeStamp=Long.parseLong(getCurrentQuarterStartTime());
        break;
      case 5:
        timeStamp=Long.parseLong(getNowTime(DATE_YYYY)+"0101");
        break;
      default:
        break;

    }

    return timeStamp;
  }

  /**
   * 本周第一天
   * @return
   */
  public static String getWeekMonday(){
    SimpleDateFormat formater=new SimpleDateFormat(DATE_SMALL_DD);
    Calendar cal=new GregorianCalendar();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setTime(new Date());
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    Date first=cal.getTime();
    return formater.format(first);
  }


  public static void main(String[] args) {
    System.out.println(getCurrentQuarterStartTime());
  }

  /**
   * 当月第一天
   * @return
   */
  public static String getMonthFirst(){
    SimpleDateFormat formater=new SimpleDateFormat(DATE_SMALL_DD);
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, 0);
    c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
    String first = formater.format(c.getTime());
    return first;
  }
  /**
   * 季度第一天
   * @return
   */
  public static String getCurrentQuarterStartTime() {
    SimpleDateFormat formater=new SimpleDateFormat(DATE_SMALL_DD);
    Calendar c = Calendar.getInstance();
    int currentMonth = c.get(Calendar.MONTH) + 1;
    SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    Date now = null;
    try {
      if (currentMonth >= 1 && currentMonth <= 3)
        c.set(Calendar.MONTH, 0);
      else if (currentMonth >= 4 && currentMonth <= 6)
        c.set(Calendar.MONTH, 3);
      else if (currentMonth >= 7 && currentMonth <= 9)
        c.set(Calendar.MONTH, 4);
      else if (currentMonth >= 10 && currentMonth <= 12)
        c.set(Calendar.MONTH, 9);
      c.set(Calendar.DATE, 1);
      now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
    } catch (Exception e) {
      e.printStackTrace();
    }
    return formater.format(now);
  }

}
